import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pro/routes/my_chart_page.dart';
import 'package:flutter_pro/routes/page_more.dart';
import 'package:flutter_pro/routes/tab_view.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../widgets/center_cell.dart';
import 'calculator.dart';
import 'candle_page.dart';
import 'chart_detail.dart';
import 'coin_page.dart';

class MinePageView extends StatefulWidget {
  const MinePageView({super.key});

  @override
  State<StatefulWidget> createState() => MinePageState();
}

class MinePageState extends State<MinePageView>
    with AutomaticKeepAliveClientMixin {
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  late int orderNum = 2;

  @override
  void initState() {
    // TODO: implement setState
    super.initState();
    print("MinePageState initState");
  }

  void _onRefresh() async {
    // monitor network fetch
    await Future.delayed(const Duration(milliseconds: 1000));
    // if failed,use refreshFailed()
    orderNum = 2;
    setState(() {});

    _refreshController.loadComplete();
    _refreshController.refreshCompleted();
  }

  void _onLoading() async {
    // monitor network fetch
    await Future.delayed(const Duration(milliseconds: 1000));
    // if failed,use loadFailed(),if no data return,use LoadNodata()
    orderNum += 2;
    setState(() {});

    if (orderNum >= 10) {
      _refreshController.loadNoData();
    } else {
      _refreshController.loadComplete();
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    // TODO: implement build
    return Scaffold(
      backgroundColor: const Color(0xfff5f5f5),
      appBar: AppBar(
        title: const Text("我的"),
        centerTitle: true,
        elevation: 0.0,
      ),
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: const WaterDropHeader(),
        footer: CustomFooter(
          builder: (BuildContext context, LoadStatus? mode) {
            Widget body;
            if (mode == LoadStatus.idle) {
              body = const Text("上拉加载");
            } else if (mode == LoadStatus.loading) {
              body = const CupertinoActivityIndicator();
            } else if (mode == LoadStatus.failed) {
              body = const Text("加载失败！点击重试！");
            } else if (mode == LoadStatus.canLoading) {
              body = const Text("松手,加载更多!");
            } else {
              body = const Text("没有更多数据了!");
            }

            return SizedBox(
              height: 55.0,
              child: Center(child: body),
            );
          },
        ),
        controller: _refreshController,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: ListView(
          children: [
            Container(
              height: 200,
              color: Colors.lightBlueAccent,
            ),
            CenterCell(
              title: "绘制",
              onTap: () {
                Get.to(const MyChartPage(), transition: Transition.rightToLeft);
              },
            ),
            CenterCell(
              title: "币种",
              number: orderNum.toString(),
              onTap: () {
                Get.to(const CoinPage(), transition: Transition.rightToLeft);
              },
            ),
            CenterCell(
                title: "新闻",
                onTap: () {
                  Get.toNamed("/newsPage");
                }),
            CenterCell(
                title: "多页面",
                onTap: () {
                  Get.to(const PageMoreWidget());
                }),
            CenterCell(
                title: "多页面2",
                onTap: () {
                  Get.to(const TabViewPage());
                }),
            CenterCell(
                title: "走势",
                onTap: () {
                  Get.to(const CandlePageWidget());
                }),
            CenterCell(
                title: "chart".tr,
                onTap: () {
                  Get.to(const ChartDetail());
                }),
            CenterCell(
                title: "calculator".tr,
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const CalculatorPage()));
                }),
            CenterCell(
              title: "languageSet".tr,
              number: 'Hello'.tr,
              onTap: () {
                var locale = const Locale('en', 'US');
                //当前语言环境
                if (Get.locale?.languageCode == "en") {
                  locale = const Locale('zh', 'CN');
                }

                orderNum += 1;
                Get.updateLocale(locale);
              },
            ),
          ],
        ),
      ),
    );
  }

  // String getCurrentLanguage() {
  //   Locale locale = window.locale;
  //   return locale.languageCode; // 返回语言代码
  // }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
